'use strict';
app.controller('propertyCtrl', ['$scope', 'utilService', 'propertyService', 'toaster', '$modal','$state',
    function ($scope, utilService, propertyService, toaster, $modal,$state) {
    $scope.shopWithdrawalHistoryVO = {
        state: 0
    };
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + "withdrawal",
            params: $scope.shopWithdrawalHistoryVO
        }).then(function (result) {
        })
    };
    $scope.query();

    //查询店铺信息
    $scope.shop = {};
    $scope.findShop = function () {
        propertyService.findShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shop = result.data;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findShop();

    //查询店铺是否绑定银行卡
    $scope.getShopBankcardList = function () {
        propertyService.findAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    var info = result.data;
                    if (info.length > 0) {
                        //跳转到余额提现
                        $state.go('app.withdraw');
                    } else {
                        //跳转到模态框
                        openBankcardBoundPrompt();
                    }
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
        function openBankcardBoundPrompt() {
            utilService.modal($modal, $scope, {
                title: "信息提示",
                controller: 'bankcardBoundPromptCtr',
                message: '当前店铺未绑定银行卡,是否绑定?'
            })
        }
    };

    $scope.withdrawalConfirm=function(id){
        var param={
            id:id
        };
        openModal("确定提现成功吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"修改状态",
                controller:"withdrawalConfirmCtrl",
                message:msg,
                data:param
            });
        }
    }
}]);

app.controller('withdrawCtrl', ['$scope', 'utilService', 'propertyService', 'toaster', '$modal',
    function ($scope, utilService, propertyService, toaster, $modal) {
    $scope.withdraw = {};
    $scope.max = 0;
    //查询店铺信息
    $scope.findShop = function () {
        propertyService.findShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.max = result.data.withdrawalMoney;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findShop();

    //查询银行卡信息
    $scope.bank = {};
    $scope.findAll = function () {
        propertyService.findAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    var info = result.data;
                    for (var i = 0; i < info.length; i++) {
                        info[i].bankCard = info[i].bankCard.substring(0, 4) + ' **** **** *** ' + info[i].bankCard.substring(15, 19);
                    }
                    $scope.bank = info;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
    $scope.findAll();

    //获取当前店铺的主体账号手机
    $scope.findMainPhone = function () {
        propertyService.findMainPhone($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.withdraw.alterPhone = result.data.substring(0, 3) + '****' + result.data.substring(7, 11);
                    $scope.withdraw.sendPhone = result.data;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
    $scope.findMainPhone();

    //打开确认框
    $scope.openAdd = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/property/account_mobile_phone_check.html',
            controller: 'addHistoryCtrl',
            size: 'sm',
            scope: $scope,
            resolve: {
                data: function () {
                    var data = $scope.withdraw;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

}]);

app.controller('addHistoryCtrl', ['$scope', '$state', 'propertyService', 'toaster', 'data', '$modalInstance',
    function ($scope, $state, propertyService, toaster, data, $modalInstance) {
    $scope.data = data;

    $scope.confirm = function () {
        propertyService.confirm($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.property');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    $scope.sendSms = function (mobilePhone) {
        propertyService.getMobilePhoneCode($scope, mobilePhone).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "验证码已发送,请注意查收");
                    break;
                case 680:
                    toaster.pop('error', "操作失败", "不能频繁发送验证码,请稍后再发");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
    $scope.sendSms($scope.data.sendPhone);
}]);

app.controller('bankcardBoundPromptCtr', ['$scope', '$modalInstance','modalData','$state', function ($scope, $modalInstance,modalData,$state) {
    $scope.modal = modalData;
    $scope.ok = function () {
        //加载添加银行卡页面
        $state.go('app.bankCard');
        $modalInstance.dismiss('success');
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }
}]);

app.controller('propertyHistoryCtrl', ['$scope', 'utilService', 'propertyService', 'toaster', '$modal','$state','$filter',
    function ($scope, utilService, propertyService, toaster, $modal,$state,$filter) {
    $scope.propertyHistoryVO = {};

    $scope.convert = function(){
        if($scope.propertyHistoryVO.startTime!=null){
            $scope.propertyHistoryVO.startTime = $filter("date")($scope.propertyHistoryVO.startTime, "yyyy-MM-dd");
        }
        if($scope.propertyHistoryVO.endTime!=null){
            $scope.propertyHistoryVO.endTime = $filter("date")($scope.propertyHistoryVO.endTime, "yyyy-MM-dd");
        }
    };

    $scope.query = function () {
        $scope.convert();
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + "withdrawal/property/history",
            params: $scope.propertyHistoryVO
        }).then(function (result) {
        })
    };
    $scope.query();
}]);

app.controller('withdrawalConfirmCtrl',['$scope','$modalInstance','propertyService','toaster','modalData',function($scope,$modalInstance,propertyService,toaster,modalData){
    $scope.modal=modalData;
    $scope.withdrawal={
        id:modalData.data.id,
        status:2,
        confirmType:1
    };
    $scope.isDelModal=true;
    $scope.ok = function(){
        if(!$scope.isDelModal){
            return;
        }
        $scope.isDelModal = false;
        propertyService.change($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success',"提示","操作成功");
                    $scope.isDelModal=true;
                    $scope.query();
                    break;
                default:
                    toaster.pop('error',"操作失败","发生未知错误!请联系管理员");
                    break;
            }
        });
    }
    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

app.controller('goodsHistoryCtrl', ['$scope', 'utilService', 'propertyService', 'toaster','$stateParams',
    function ($scope, utilService, propertyService, toaster,$stateParams) {

    $scope.goodsHistory={
        ordersDataHistoryId:$stateParams.id
    };
    $scope.query = function(){
        propertyService.query($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsHistory = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.query();
}]);
